package com.freemyleft.left.zapp.selectimage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.ExifInterface;


import com.freemyleft.left.zapp.selectimage.bean.Image;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by Administrator on 2017/6/15.
 */

public class

FileUtils {
    private static String mFile = "/sdcard/imageselect/";

    public static void deleteFile(File file) {
        if (file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                file.delete(); // delete()方法 你应该知道 是删除的意思;
            } else if (file.isDirectory()) { // 否则如果它是一个目录
                File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
                for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
                    deleteFile(files[i]); // 把每个文件 用这个方法进行迭代

                }
            }
            file.delete();
        } else {
        }
    }
    /**
     * 删除文件夹所有内容
     */
    public static void deleteFile() {
        deleteFile(new File(mFile));
    }
    /**
     * 保存到指定路径
     */
    public static String saveNativePath(Bitmap bit, String imageName) {
        File filePath = new File(mFile);
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        String imagePath= mFile + imageName;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(imagePath);
            bit.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return imagePath;
    }

    public static Image getImage(Activity activity, String path, String imageName) {
        ExifInterface exifInterface = null;
        Image image = null;
        try {
            exifInterface = new ExifInterface(path);
            int height = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, BitmapUtils.getScreenHight(activity));
            int width = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, BitmapUtils.getScreenWidth(activity));
            image = new Image(path, imageName , System.currentTimeMillis(),width, height );
        } catch (IOException e) {
            image = new Image(path, "", System.currentTimeMillis(), BitmapUtils.getScreenHight(activity), BitmapUtils.getScreenWidth(activity));
        }
        return image;
    }

}
